package com.laihui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.StringUtils;
import com.laihui.Constants;
import com.laihui.MainActivity;
import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.base.BaseActivity;
import com.laihui.entity.Login;
import com.laihui.utils.CodeUtils;
import com.laihui.utils.LoginPwdFormatUtils;
import com.laihui.utils.ToastUtil;

import com.laihui.view.TopBar;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 注册第一个页面
 */
public class RegistActivityNew extends BaseActivity {



    @BindView(R.id.topbar)//topbar
            TopBar topBar;
    @BindView(R.id.et_phone)//手机号
            EditText etPhone;



    @BindView(R.id.et_password)//设置密码
            EditText etPassword;

    @BindView(R.id.et_repassword)//设置密码
            EditText etRepassword;



    @BindView(R.id.et_invite)//邀请码
            EditText etInvite;


    @BindView(R.id.btn_regist_next)//登录按钮
            Button btnRegistNext;

    HashMap map;
    CodeUtils codeUtils;
    private Activity mActivity;
    private MyApplication application;
    private static String Tag = "RegistActivity";
    private LoginPwdFormatUtils loginPwdFormatUtils;

    @Override
    public void init() {
        mActivity = RegistActivityNew.this;
        application = (MyApplication) getApplication();
        topBar.setRightButton(true);


        topBar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                startActivity(new Intent(mActivity, MainActivity.class));
                finish();
            }

            @Override
            public void rightClick() {
                startActivity(new Intent(mActivity, LoginActivity.class));
            }
        });


    }




    @SuppressLint("LongLogTag")
    private void registPerson() {
        String et_phone = etPhone.getText().toString().trim();
        if (et_phone.isEmpty()) {
            ToastUtil.showToast(mActivity, "请输入手机号");
            return;
        }
        String et_password = etPassword.getText().toString().trim();
        if (et_password.isEmpty()) {
            ToastUtil.showToast(mActivity, "请输入密码");
            return;
        }
        String et_repassword = etRepassword.getText().toString().trim();
        if (et_repassword.isEmpty()) {
            ToastUtil.showToast(mActivity, "请在此输入密码");
            return;
        }
        if(!et_password.equals(et_repassword)){
            ToastUtil.showToast(mActivity, "请验证2次密码是否相同");
            return;
        }
        String et_invite = etInvite.getText().toString().trim();
        HashMap map = new HashMap();
        map.put("mobile",et_phone);
        map.put("pass1",et_password);
        if(!et_invite.isEmpty())
        map.put("inviter",et_invite);

        application.apiService.Register(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Login>() {
                    @Override
                    public void onCompleted() {
                        //dismissProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("loginFailed", e.toString()

                        );
                    }

                    @Override
                    public void onNext(Login user) {
                        Toast.makeText(mActivity, user.message, Toast.LENGTH_SHORT).show();
                        if (user.status.equals("1")) {
//                            ToastUtil.showToast(mActivity, "登陆成功");
                            application.updateLoginParams(true, String.valueOf(user.member.member_id),etPhone.getText().toString(),user.member.mobile_status);
                            startActivity(new Intent(mActivity,RegistActivityStep2.class));
                            finish();
                        }
                    }
                });

    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_regist_new;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }


    @OnClick({R.id.btn_regist_next})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_regist_next:
                registPerson();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(Tag);
    }
}
